package biznussoft.metercor.utils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import biznussoft.metercor.R;


public class ExpandCollapseAnimation extends Animation {

    public final static int COLLAPSE = 1;
    public final static int EXPAND = 0;
   
    private View mView;
    private int mEndHeight;
    private int mType;
    private ViewGroup.LayoutParams mLayoutParams;
    ImageView expandCollapseView;

    public ExpandCollapseAnimation(View view, int duration, int type,ImageView expandCollapseTextView) {

        setDuration(duration);
        mView = view;
        expandCollapseView= expandCollapseTextView ;
        mEndHeight = mView.getHeight();
        mLayoutParams = ((ViewGroup.LayoutParams) view.getLayoutParams());
        mType = type;
        if(mType == EXPAND) {
            mLayoutParams.height =0;
        } else {
            mLayoutParams.height = LayoutParams.WRAP_CONTENT;
        }
        view.setVisibility(View.VISIBLE);
       
    }

    public int getHeight(){
        return mView.getHeight();
    }

    public void setHeight(int height){
    	
        mEndHeight = height;  
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
        	
            if(mType == EXPAND) {
            	
                mLayoutParams.height =  (int)(mEndHeight * interpolatedTime);
                if(expandCollapseView!=null)
           	    expandCollapseView.setImageResource(R.drawable.collapse);
            } else {
                mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime));
                if(expandCollapseView!=null)
                expandCollapseView.setImageResource(R.drawable.expand);                    
            }
            mView.requestLayout();
        } else {
        	
            if(mType == EXPAND) {
            	 mView.setVisibility(View.VISIBLE);
                mLayoutParams.height = LayoutParams.WRAP_CONTENT;
                mView.requestLayout();
                expandCollapseView.setImageResource(R.drawable.collapse);
            }else{
                mView.setVisibility(View.GONE);
            }
        }
    }
}